//gulp配置文件

var gulp = require('gulp');


//  "gulp": "^3.9.1",
//  "gulp-autoprefixer": "^4.0.0",
//  "gulp-concat": "2.6.1",
//  "gulp-connect": "",
//  "gulp-htmlmin": "",
//  "gulp-imagemin": "",
//  "gulp-jslint": "",
//  "gulp-rename": "",
//  "gulp-ruby-sass": "",
//  "gulp-uglify": "^2.0.0"
var sass = require('gulp-ruby-sass');
//增加浏览器前缀
var autoprefixer = require('gulp-autoprefixer');

var concat = require('gulp-concat');
var uglify = require('gulp-uglify');

//压缩html插件
var htmlmin = require('gulp-htmlmin');

//压缩图片插件
var imagemin = require('gulp-imagemin');


//connect  刷新浏览器
var connect = require('gulp-connect');


gulp.task('imagemin',function(){
	gulp.src('./src/image/*').pipe(imagemin()).pipe(gulp.dest('./dist/image'));
})


//压缩html任务
gulp.task('htmlmin',function(){
	gulp.src('./src/html/**/*.html').pipe(htmlmin({collapseWhitespace: true})).pipe(gulp.dest('./dist/html'))
})


//编译 压缩 sass
gulp.task('sass',function(){
	
//	gulp.src('./src/sass/**/*.scss')
//	.pipe(sass({style:'compressed'}))
//	.pipe(gulp.dest('./dist/css'))

	sass('./src/sass/**/*.scss',{style:'compressed'}).pipe(autoprefixer()).pipe(gulp.dest('./dist/css'));
	
});


//js代码 压缩

// 检查语法错误
//var jslint = require('gulp-jslint');

gulp.task('uglify',function(){
	//pipe(jslint()).pipe(jslint.reporter('default')).
	
	gulp.src('./src/js/**/*.js').pipe(uglify()).pipe(gulp.dest('./dist/js'));
});


//合并js
//concat

//gulp.task('concat',function(){
//	gulp.src(['./src/js/ajax.js','./src/js/login.js'])
//	.pipe(concat('login.min.js')).pipe(uglify()).pipe(gulp.dest('./dist/js'));
//})


//自动刷新浏览器
gulp.task('connect',function(){
	gulp.src('./dist/html/**/*').pipe(connect.reload());
})

gulp.task('start',function(){
	
	connect.server({
		livereload:true
	});
	
	//如果文件发生变化，就执行后面的任务
	gulp.watch('./src/sass/**/*.scss',['sass']);
	gulp.watch('./src/js/**/*.js',['uglify','concat']);
//	gulp.watch('./src/js/**/*.js',['uglify']);

	gulp.watch('./src/html/**/*.html',['htmlmin']);
	
	gulp.watch('./src/image/*',['imagemin']);
	
	
	gulp.watch('./src/**/*',['connect']);
	
})
